﻿using VirtualTournaments.DataAccess.SqlServer.DataProxies;

namespace VirtualTournaments.DataAccess.SqlServer.DataMappers
{
	internal sealed class PlayersDataMapper : AbstractDataMapper<SqlServer.Player, Business.Players.Player, DataProxies.PlayerDataProxy>
	{
		#region Singleton implementation

		private static PlayersDataMapper _instance = null;
		private static object _instanceLocker = new object();

		private PlayersDataMapper() { }

		public static PlayersDataMapper Instance
		{
			get
			{
				if (_instance == null)
				{
					lock (_instanceLocker)
					{
						if (_instance == null)
						{
							_instance = new PlayersDataMapper();
						}
					}
				}
				return _instance;
			}
		}

		#endregion

		internal override VirtualTournamentsDataContext GetDataContext()
		{
			return VirtualTournamentsDataContext.Instance;
		}

		internal override System.Data.Linq.Table<Player> GetDataTable()
		{
			return GetDataContext().Players;
		}

		internal override Business.Players.Player DataObject2BusinessObject(SqlServer.Player dataObject)
		{
			return new PlayerDataProxy(dataObject);
		}

		internal override void BusinessObject2DataObject(Business.Players.Player bizPlayer, SqlServer.Player dataPlayer)
		{
			dataPlayer.FirstName = bizPlayer.Name.FirstName;
			dataPlayer.Surname = bizPlayer.Name.Surname;
			dataPlayer.Role = (int)bizPlayer.Role;
			dataPlayer.Speed = bizPlayer.Speed;
			dataPlayer.Dribbling = bizPlayer.Dribbling;
			dataPlayer.Strength = bizPlayer.Strength;
			dataPlayer.Precision = bizPlayer.Precision;
		}

		internal override void DataProxy2DataObject(PlayerDataProxy proxyPlayer, Player dataPlayer)
		{
			//TODO: untested
			dataPlayer.FirstName = proxyPlayer.Name.FirstName;
			dataPlayer.Surname = proxyPlayer.Name.Surname;
			dataPlayer.Role = (int)proxyPlayer.Role;
			dataPlayer.Speed = proxyPlayer.Speed;
			dataPlayer.Dribbling = proxyPlayer.Dribbling;
			dataPlayer.Strength = proxyPlayer.Strength;
			dataPlayer.Precision = proxyPlayer.Precision;
		}
	}
}
